<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省市区" prop="provincial">
        <el-input v-model="queryParams.provincial" placeholder="请输入省市区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>   
      <el-form-item v-for="item in dynamicParamList" :key="item.filedName" :label="item.name" :prop="item.filedName">
        <el-select multiple style="width: 100%" v-model="form[item.filedName]" :placeholder="`请选择${item.name}`"
          @change="handleQuery" :allow-clear="true" clearable>
          <el-option v-for="itemOp in item.children" :key="itemOp.value" :label="itemOp.name" :value="itemOp.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="年龄" >
        <div class="flex">
          <el-input-number :min="0" v-model="queryParams.minAge"  />
          <div style="margin: 0 8px">至</div>
          <el-input-number :min="0" v-model="queryParams.maxAge"  />
        </div>
      </el-form-item>

      <el-form-item label="注册时间">
        <MobileDatePicker v-model="queryParams.dateRange" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-place" size="small" @click="openCity">城市分布图</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="numberTempList">
  <el-table-column label="用户ID" align="center" prop="id" width="170">
    <template slot-scope="scope">
      <span class="clickable-uid" @click="handleDetailsDialog(scope.row)">{{ scope.row.id }}</span>
    </template>
  </el-table-column>
  <el-table-column label="应用信息" align="center" width="160">
    <template slot-scope="scope">
      <div>[{{ scope.row.appCode }}] {{ scope.row.appName }}</div>
    </template>
  </el-table-column>
  <el-table-column label="个人信息" align="center" width="155">
    <template slot-scope="scope">
      <div>{{ scope.row.realName }} - {{ scope.row.sex === '1' ? '男' : '女' }} - {{ scope.row.age }}岁</div>
    </template>
  </el-table-column>
  <el-table-column label="所属地" align="center" width="120">
    <template slot-scope="scope">
      <div>[{{ scope.row.province }}] {{ scope.row.city }}</div>
    </template>
  </el-table-column>
  <el-table-column label="贷款信息" align="center" width="150">
    <template slot-scope="scope">
      <div>{{ scope.row.loanTime }} / {{ scope.row.loanAmount }}</div>
    </template>
  </el-table-column>
  <el-table-column label="房 / 车" align="center" width="160">
    <template slot-scope="scope">
      <div>{{ scope.row.house }} / {{ scope.row.car }}</div>
    </template>
  </el-table-column>
  <el-table-column label="社保 / 公积金" align="center" width="160">
    <template slot-scope="scope">
      <div>{{ scope.row.shebao }} / {{ scope.row.gjj }}</div>
    </template>
  </el-table-column>
  <el-table-column label="芝麻分 / 保险" align="center" width="170">
    <template slot-scope="scope">
      <div>{{ scope.row.zhima }} / {{ scope.row.baoxian }}</div>
    </template>
  </el-table-column>
  <el-table-column label="其他资质" align="center" width="300">
    <template slot-scope="scope">
      <div>{{ scope.row.huabei }} / {{ scope.row.baitiao }} / {{ scope.row.weili }} / {{ scope.row.shouru }} / {{ scope.row.xueli }} / {{ scope.row.yongtu }} / {{ scope.row.yuqi }} / {{ scope.row.xinyongka }} / {{ scope.row.gongzuo }}</div>
    </template>
  </el-table-column>
  <el-table-column label="用户IP" align="center" prop="userIp" width="120">
    <template slot-scope="scope">
      {{ scope.row.userIp }}
    </template>
  </el-table-column>
  <el-table-column label="创建时间" align="center" width="150">
    <template slot-scope="scope">
      {{ formatTimestamp(scope.row.createTime) }}
    </template>
  </el-table-column>
</el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 用户详情弹窗 -->
    <Member ref="MemberRef" />


    <el-dialog :visible="open" @close="open = false" title="城市分布图:" width="50%">
      <City ref="CityRef" />
    </el-dialog>
  </div>
</template>

<script>
import { listHistory} from "@/api/source/history";
import { listTaskDictInfo } from "@/api/dict/dictInfo";
import { baseInfoExtData } from "@/api/source/baseInfo";
import Member from '@/views/source/memeber.vue';
import dayjs from 'dayjs';
import City from '@/views/source/city/city.vue'
export default {
  name: "NumberTemp",
  components: {
    Member,
    City
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      showDialog: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线索用户资质信息表格数据
      numberTempList: [],
      numberTempLists: [],
      dynamicParamList: [],
      extData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        realName: null,
        realNameMd5: null,
        mobile: null,
        mobileMd5: null,
        idCard: null,
        idCardMd5: null,
        age: null,
        sex: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        userIp: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
        shouru: null,
        xueli: null,
        yongtu: null,
        yuqi: null,
        xinyongka: null,
        orFlag: null,
        gongzuo: null,
        startCreateTime: null,
        endCreateTime: null,
        dateRange: [`${dayjs().format('YYYY-MM-DD')} 00:00:00`, `${dayjs().format('YYYY-MM-DD')} 23:59:59`],
      },
      // 表单参数
      form: {
        orFlag: '0'
      },
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        channelCode: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ],
        channelName: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        mobileMd5: [
          { required: true, message: "手机号MD5不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getOptions();
  },
  methods: {
    /** 查询线索用户资质信息列表 */
    getList() {
      this.loading = true;
      const sata = { ...this.queryParams }
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startDate = this.queryParams.dateRange[0];
        const endDate = this.queryParams.dateRange[1];
        sata.startCreateTime = startDate;
        sata.endCreateTime = endDate;
      }
      delete sata.dateRange
      listHistory(sata).then(response => {
        this.numberTempList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },

    async handleDetailsDialog(row) {
      this.$refs.MemberRef.show(row.id)
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        realName: null,
        realNameMd5: null,
        mobile: null,
        mobileMd5: null,
        idCard: null,
        idCardMd5: null,
        age: null,
        sex: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        userIp: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
        shouru: null,
        xueli: null,
        yongtu: null,
        yuqi: null,
        xinyongka: null,
        gongzuo: null,
        minAge: null,
        maxAge: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    /**更改创建时间格式 */
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return formattedDate;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      for (const item of this.dynamicParamList) {
        const fieldName = item.filedName;
        const selectedValues = this.form[fieldName];
        if (Array.isArray(selectedValues)) {
          this.queryParams[fieldName] = selectedValues.join(',');
        } else {
          this.queryParams[fieldName] = selectedValues;
        }
      }
      this.getList();
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listTaskDictInfo().then((response) => {
        this.dynamicParamList = this.handleTree(response.rows, "id");
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("form");
      for (const item of this.dynamicParamList) {
        this.form[item.filedName] = null;
      }
      this.queryParams.dateRange = [];
      this.queryParams.uid = null;
      this.queryParams.mobile = null;
      this.queryParams.provincial = null;
      this.queryParams.appName = null;
      this.queryParams.channelName = null;
      this.queryParams.minAge = null;
      this.queryParams.maxAge = null;
      this.handleQuery();
    },



    openCity() {
      this.open = true;
    },
  
 

  }
};
</script>
<style scoped>
.clickable-uid {
  cursor: pointer;
  color: blue;
}
</style>